<template>
  <div class="home">
    <el-container>
      <el-header>
        <div>
          <img src="../assets/logo2.png" alt="" />
          <h3>梦学谷会员管理系统</h3>
        </div>
        <div>
          <el-button type="info" @click="exit">退出</el-button>
          <el-button type="info" @click="dialogVisible = true"
            >修改密码</el-button
          >
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu
            default-active="2"
            class="el-menu-vertical-demo"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
            router
          >
            <el-menu-item
              :index="'/' + item.path"
              v-for="item in leftList"
              :key="item.id"
            >
              <span slot="title">{{ item.authName }}</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
          <!-- 修改密码弹框 -->
          <el-dialog title="修改密码" :visible.sync="dialogVisible" width="30%">
            <el-form
              :model="ruleForm"
              :rules="rules"
              ref="ruleForm"
              label-width="100px"
              class="demo-ruleForm"
            >
              <el-form-item label="原密码" prop="oldPassword">
                <el-input
                  v-model="ruleForm.oldPassword"
                  type="password"
                ></el-input>
              </el-form-item>
              <el-form-item label="新密码" prop="newPassword">
                <el-input
                  v-model="ruleForm.newPassword"
                  type="password"
                ></el-input>
              </el-form-item>
              <el-form-item label="确认密码" prop="password">
                <el-input
                  v-model="ruleForm.password"
                  type="password"
                ></el-input>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="amend">确 定</el-button>
            </span>
          </el-dialog>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.password !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.newPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      leftList: [],
      activeName: "Welcome",
      dialogVisible: false,
      ruleForm: {
        oldPassword: "",
        newPassword: "",
        password: "",
      },
      rules: {
        oldPassword: [
          { required: true, message: "原密码不能为空", trigger: "blur" },
          { min: 1, max: 5, message: "长度在 1 到 5 个字符", trigger: "blur" },
        ],
        newPassword: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 1, max: 6, message: "请输入1-6位字符", trigger: "blur" },
          { validator: validatePass, trigger: "blur" },
        ],
        password: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          { validator: validatePass2, trigger: "blur", required: true },
        ],
      },
    };
  },
  async created() {
    let res = await this.axios("./list.json");
    this.leftList = res.data.data;
  },
  methods: {
    exit() {
      this.$router.push("/Login");
    },
    amend() {
      this.$refs.ruleForm.validate((vali) => {
        if (vali) {
          this.$router.push("/Login");
        } else {
          return;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100%;
  .el-container {
    width: 100%;
    height: 100%;
  }
  .el-header {
    background-color: #2d3a4b;
    color: #333;
    text-align: center;
    line-height: 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div:nth-child(1) {
      width: 100%;
      height: 100%;
      display: flex;
      color: white;
      img {
        width: 60px;
        height: 60px;
        border-radius: 50%;
        margin-right: 10px;
      }
    }
    div:nth-child(2) {
      height: 100%;
      display: flex;
    }
  }

  .el-aside {
    background-color: #545c64;
    color: #333;
    text-align: center;
    line-height: 200px;
  }

  .el-main {
    background-color: #e9eef3;
    color: #333;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }
}
</style>
